package com.dream.system.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.persistence.Table;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StringType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.stereotype.Component;

import com.dream.common.BuildEntityClass;
import com.dream.common.ClassUtil;
import com.dream.common.DateTools;
import com.dream.common.FreemakerUtil;
import com.dream.common.SpringContextHolder;
import com.dream.common.Super;
import com.dream.common.SuperDao;
import com.dream.model.Table_Info;
import com.dream.system.service.DynamicFormService;

@Component("dynamicFormServiceImpl")
@SuppressWarnings("all")
public class DynamicFormServiceImpl extends Super<Object> implements DynamicFormService{

	@Override
	public void createTable(String createTableSql) {
		// TODO Auto-generated method stub
		this.createTableBySQL(createTableSql);
	}
	@Override
	public Map<String, Object> getMap(String tableName,JSONObject conditionJson,int start,int limit) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new  StringBuffer();
		
		for(Object key:conditionJson.keySet()){
			
			if(StringUtils.isNotEmpty(conditionJson.getString(String.valueOf(key)))){

				//sql.append(" and "+key+" like ? ");
				//params.add(conditionJson.get(key)+"%");
				sql.append(" select table_id from  " + tableName +" sch where  sch."+key+" like ?");
				sql.append(" union all ");
				params.add(conditionJson.get(key)+"%");
			}
		}
		String resultSql = "";
		if(sql.toString().contains("union")){
			resultSql = sql.substring(0, sql.lastIndexOf("union"));
		}else{
			resultSql = sql.toString();
		}
		
		long startDate = DateTools.getCurrentDateLong();
		
		List list = this.getListByTableName(tableName,params.toArray(),resultSql,start,limit);
		
		int sum = this.getListCountByTableName(tableName, params.toArray(), resultSql, 0, 0);
		int count = (int)Math.ceil(Double.valueOf(sum)/Double.valueOf(limit));
		
		System.out.println(DateTools.getCurrentDateLong() - startDate);
		map.put("list", list);
		map.put("sum", list.size());
		map.put("count", count);
		
		return map;
	}
	@Override
	public void insert(JSONObject json, String tableName) {
		// TODO Auto-generated method stub
		this.addSql(tableName, json);
	}
	@Override
	public void update(JSONObject json, String tebleName) {
		// TODO Auto-generated method stub
		this.updateSql(tebleName, json);
	}
	@Override
	public void delete(String tableName, JSONObject params) {
		// TODO Auto-generated method stub
		this.deleteSql(tableName, params);
	}
	@Override
	public void insertTableInfo(Table_Info model) {
		// TODO Auto-generated method stub
		this.addT(model);
	}
	@Override
	public List<Table_Info> getTabelInfoList() {
		// TODO Auto-generated method stub
		String sql = "from Table_Info t";
		List<Table_Info> list = this.getList(sql, 0, 0, "");
		
		return list;
	}
	@Override
	public Table_Info getTableInfoByEnName(String tableName) {
		// TODO Auto-generated method stub
		String sql = "from Table_Info t where t.table_en_name = ? ";
		List<Table_Info> list = this.getList(sql,new Object[]{tableName}, 0, 0, "");
		if(list.size() <= 0){
			return new Table_Info();
		}
		return list.get(0);
	}
}
